package com.plian.system.service.yth.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.system.entity.ms.MonthPlan;
import com.plian.system.entity.yth.ms.YthMonthPlan;
import com.plian.system.mapper.yth.ms.YthMonthPlanMapper;
import com.plian.system.service.yth.IYthMonthPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2023/6/9 10:08 AM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Slf4j
@Service
public class YthMonthPlanServiceImpl extends ServiceImpl<YthMonthPlanMapper, YthMonthPlan> implements IYthMonthPlanService {

    private final String YTH_DATASOURCE_TYPE = "3";

    @Transactional(rollbackFor = Exception.class)
//    @DS("slave_1")
    @Override
    public List<MonthPlan> getAllMonth(Date startTime, Date endTime) {
        List<MonthPlan> monthPlans = new ArrayList<>();

        List<YthMonthPlan> oldData = list(new LambdaQueryWrapper<YthMonthPlan>()
                .gt(Optional.ofNullable(startTime).isPresent(),YthMonthPlan::getCreateTime,startTime)
                .le(Optional.ofNullable(endTime).isPresent(),YthMonthPlan::getCreateTime,endTime)
        );
        if (CollectionUtil.isNotEmpty(oldData)){
            monthPlans = conversion(oldData);
        }

        return monthPlans;
    }

    public List<MonthPlan> conversion(List<YthMonthPlan> oldData){
        List<MonthPlan> monthPlans = new ArrayList<>();
        ZoneId zone = ZoneId.systemDefault();
        for (YthMonthPlan ythMonthPlan:oldData) {
            MonthPlan monthPlan = new MonthPlan();
            monthPlan.setCode(ythMonthPlan.getInvoiceCode());
            monthPlan.setMonthPlan(ythMonthPlan.getNextMonthPlan());
            monthPlan.setMonthSummary(ythMonthPlan.getMonthSummary());
            monthPlan.setAttachIds(ythMonthPlan.getAttachIds());
            monthPlan.setDataSource(YTH_DATASOURCE_TYPE);
            monthPlan.setDataSourceId(ythMonthPlan.getId().toString());
            monthPlan.setStatus(3);
            Optional.ofNullable(ythMonthPlan.getReportTime()).ifPresent(m ->{
                monthPlan.setFillingMonth(DateUtil.format(Date.from(m.atStartOfDay().atZone(zone).toInstant()),DateUtil.PATTERN_MONTH));
            });
            Optional.ofNullable(ythMonthPlan.getReportOrgId()).ifPresent(m ->{
                monthPlan.setFillCompanyId(m.toString());
            });
            Optional.ofNullable(ythMonthPlan.getReportUserId()).ifPresent(m ->{
                monthPlan.setCreateUser(m.toString());
            });
            monthPlans.add(monthPlan);
        }
        return monthPlans;
    }

}
